package com.dang.apj.web.servlet.regist;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dang.apj.util.ImageUtil;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


@WebServlet("/checkCode.action")
public class SafeCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Map<String,BufferedImage> map = ImageUtil.createImage();
		String key = map.keySet().iterator().next();
		session.setAttribute("checkCode", key);
		BufferedImage image = map.get(key);
		ServletOutputStream out = response.getOutputStream();
		JPEGImageEncoder imageEncoder = JPEGCodec.createJPEGEncoder(out);
		imageEncoder.encode(image);
		out.flush();
	}

}
